package com.tamartech.greendb.util;

import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.data.ElectoralRollEntry;

public class VoterNumberConverter
{

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception
    {
        ElectoralRollEntryDAO dao = new ElectoralRollEntryDAO();
        
        for (ElectoralRollEntry e : dao.list())
        {
            if (e.getRefSuffix() != null && e.getVoterNumber() == 0)
            {
                try
                {
                    split (e, "/");
                }
                catch (NumberFormatException e1)
                {
                    split (e, "\\.");
                }
                
                dao.update(e);
            }
//                System.out.println ("e.getRefSuffix(): " + e.getRefSuffix() +
//                                    ". e.getVoterNumber(): " +e.getVoterNumber()
//                                    );
        }
    }

    private static void split (ElectoralRollEntry e, String delimiter)
    {
        System.out.println ("e.getRefSuffix(): " + e.getRefSuffix());
        String numbers[] = e.getRefSuffix().split(delimiter);
        
        e.setVoterNumber (Integer.valueOf(numbers[0]));
        
        if (numbers.length > 1)
            e.setVoterNumberSuffix (Integer.valueOf(numbers[1]));
    }
}
